<template>
	<view class="container z-font-lg">
		<view class="u-padding-20 white-bg z-b-r z-b-s">
			<view class="wkinfo-item">
				<text>项目总钻孔深度：{{ projectData.drillDeepCount || 0 | $_formattingMoney }}米</text>
			</view>
			<view class="wkinfo-item">
				<text>项目总打卡天数：{{ projectData.signCountDay || '0' }}</text>
			</view>
		</view>
		
		<view class="u-padding-20 z-b-r white-bg u-margin-top-20">
			<view class="u-padding-30 white-bg z-b-r u-flex u-row-between" style="font-weight: bold;">
				<view class="z-font-lg">{{ curWKname || '加载中……' }}</view>
				<u-button type="primary" size='mini' ripple="true" @click="selectWKShow = true">切换员工</u-button>
			</view>
			<u-select v-model="selectWKShow" mode="single-column" @confirm="selectWKConfirm" :list="workers"></u-select>
			<view class="u-padding-left-30 u-padding-right-30 u-padding-bottom-30 z-font-lg">
				<view>当前数据总钻孔深度：{{ curWKdrillDeepCount }}米</view>
				<view class="u-margin-top-20">当前数据总打卡天数：{{ curWKsignCountDay }}</view>
			</view>
			<wx-load-more ref="loadMore" @provider="provider" :pageSize="20" @resData="resData">
				<template v-slot:list="{ items }">
					<view style="border: 1rpx solid #C0C0C0;">
						<view class="th-head u-flex u-row-around">		
							<view class="th-head-row th-head-row1">孔深</view>
							<view class="th-head-row th-head-row2">孔号</view>
							<view class="th-head-row th-head-row3">打卡日期</view>
							<view class="th-head-row th-head-row4">状态</view>
						</view>
						<view 
						class="td-body u-flex u-row-around" 
						v-for="(item,index) in items" :key="index" 
						>
							<view class="td-body-row td-body-row1">{{ item.hole_deep}}</view>
							<view class="td-body-row td-body-row2">{{ item.hole_mark }}</view>
							<view class="td-body-row td-body-row3">{{ item.date }}</view>
							<view class="td-body-row td-body-row4">
								<u-tag 
									size="mini" 
									:text="item.signinStatus === 1 ? '通过' : (item.signinStatus === 0 ? '待审核' : '未通过')" 
									:type="item.signinStatus === 1 ? 'success' : (item.signinStatus === 0 ? 'info' : 'error')" 
									/>
							</view>
						</view>
					</view>
				</template>
			</wx-load-more>
		</view>
		<!-- 头部提示 -->
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import wxLoadMore from '@/components/wx-load-more/wx-load-more.vue';
	import {mapState} from "vuex";
	import {myMath} from "@/common/mathUtils.js";
	export default {
		components: {
			wxLoadMore
		},
		data() {
			return {
				selectWKShow:false,
				curWKname:'',
				curWKid:'',
				curProjectID:"",
				worker_ids:[],
				workers:[],
				projectData:{},
				curWKdrillDeepCount:0,
				curWKsignCountDay:0
			}
		},
		computed:{
			...mapState({
				token:"token",
				wkID:"wkID"
			})
		},
		onLoad(params) {
			this.curProjectID = params.projectID;
			this.getProjectData(this.curProjectID)
		},
		onReachBottom() {
			this.$refs.loadMore.reachBottom()
		},
		methods: {
			async provider(e) {
				let {token,curWKname,curWKid,curProjectID} = this;
				let {pageNo,pageSize} = e;
				this.$loading("正在加载")
				
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/wkGetWorkerInfo",
						data:{
							token,
							wkID:curWKid,
							projectID:curProjectID,
							pageNo,
							pageSize
						}
					}
				})
				uni.hideLoading();
				if (res.status === 0) {
					this.$refs.loadMore.pushData(res.data);
				} else if (res.status === 1) {
					this.$refs.loadMore.pushData([]);
				}
			},
			resData(d){
				d.forEach(item=>{
					this.curWKdrillDeepCount = myMath.accAdd(this.curWKdrillDeepCount, parseFloat(item.hole_deep))
				})
				this.curWKsignCountDay = d.length;
			},
			//获取项目信息
			async getProjectData(id){
				let {token} = this;
				this.$loading("正在加载")
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/getProjectDetail",
						data:{id}
					}
				})
				
				if(res.status === 0){
					this.worker_ids = res.data[0].worker_ids;
					this.projectData = res.data[0];
					//检测当前员工是否被当前项目踢出
					let wkHistoryProject = await this.$network({
						api:"main",
						data:{
							action:"projectAdmin/getWKhistoryProject",
							data:{id,token,type:"onProjectID"}
						}
					})
					uni.hideLoading()
					if(wkHistoryProject.status === 0){
						wkHistoryProject.data.forEach(item => {
							let {wkID,salaryWay,drillDeepCount,signCountDay} = item;
							this.worker_ids.push({wkID,salaryWay,drillDeepCount,signCountDay})
						})
					}
					
					//获取员工信息
					this.getWorkerInfo();
				}
			},
			//批量获取 员工信息
			async getWorkerInfo(){
				let {token,worker_ids} = this;
				this.$loading("正在加载");
				
				let getWKTasks = [];
				worker_ids.forEach(item=>{
					getWKTasks.push(this.$network({
						api:"main",
						data:{
							action:"projectAdmin/getWorkerInfo",
							data:{
								token,
								wkID:item.wkID
							}
						}
					}))
				});
				
				let res = await Promise.all(getWKTasks);
				// console.log(res);
				uni.hideLoading();
				if(res.length > 0){
					res.forEach(user=>{
						if(user.status === 0){
							user.data[0].label = user.data[0].name;
							user.data[0].value = user.data[0].wkID;
							this.workers.push(user.data[0]);
						}
					})
					// 初始化 第一个数据
					this.curWKname = this.workers[0].label;
					this.curWKid = this.workers[0].value;
					this.$refs.loadMore.reLoadData();
				} else {
					this.$refs.uTips.show({
						title: '无员工信息',
						type: 'warning'
					})
				}
			},
			selectWKConfirm(e){
				this.curWKname = e[0].label;
				this.curWKid = e[0].value;
				this.$refs.loadMore.reLoadData();
			}
		}
	}
</script>

<style>
	.td-body-row,
	.th-head-row{
		line-height: 60rpx;
		text-align: center;
	}
	.td-body{
		border-bottom: 1rpx solid #C0C0C0;
	}
	.td-body-row1,
	.th-head-row1,
	.td-body-row2,
	.th-head-row2,
	.td-body-row4,
	.th-head-row4
	{
		width: 20%;
	}
	.td-body-row3,
	.th-head-row3
	{
		width: 40%;
	}
	.th-head{
		font-weight: bold;
		background: #edece8;
		line-height: 80rpx;
		text-align: center;
	}
	.wkinfo-item{
		line-height: 60rpx;
		font-weight: bold;
	}
</style>
